<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/11/2
 * Time: 17:24
 */

namespace App\Admin\Models;

use Encore\Admin\Traits\ModelTree;

/**
 * App\Admin\Models\SequenceModel
 *
 * @property int                        $id         自增ID
 * @property int                        $parent_id  父ID
 * @property int                        $order      排序
 * @property string                     $name_zh    岗位职称名称-中文
 * @property string                     $name_en    岗位职称名称-英文
 * @property \Illuminate\Support\Carbon $created_at 创建时间
 * @property \Illuminate\Support\Carbon $updated_at 最后更新时间
 * @method static \Illuminate\Database\Eloquent\Builder|SequenceModel newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|SequenceModel newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|SequenceModel query()
 * @method static \Illuminate\Database\Eloquent\Builder|SequenceModel whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|SequenceModel whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|SequenceModel whereNameEn($value)
 * @method static \Illuminate\Database\Eloquent\Builder|SequenceModel whereNameZh($value)
 * @method static \Illuminate\Database\Eloquent\Builder|SequenceModel whereOrder($value)
 * @method static \Illuminate\Database\Eloquent\Builder|SequenceModel whereParentId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|SequenceModel whereUpdatedAt($value)
 * @mixin \Eloquent
 * @property int $sequence_id 序列ID
 * @property-read \Illuminate\Database\Eloquent\Collection|SequenceModel[] $children
 * @property-read int|null $children_count
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Admin\Models\EmployeeModel[] $employees
 * @property-read int|null $employees_count
 * @property-read SequenceModel $parent
 * @method static \Illuminate\Database\Eloquent\Builder|SequenceModel whereSequenceId($value)
 */
class SequenceModel extends BaseModel
{
    use ModelTree;

    protected $table = 'admin_sequences';

    protected $primaryKey = 'sequence_id';

    /**
     * SequenceModel constructor.
     *
     * @param array $attributes
     */
    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);

        $this->setTitleColumn('name_zh');
    }

    /**
     * 获取岗位职称列表
     *
     * @param array $params
     *
     * @return array
     */
    public static function getSequenceList(array $params = [])
    {
        $list = static::select(['sequence_id', 'name_zh', 'name_en']);
        if (isset($params['parent_id'])) {
            if (is_array($params['parent_id'])) {
                $list = $list->whereIn('parent_id', $params['parent_id']);
            } else {
                $list = $list->where('parent_id', '=', $params['parent_id']);
            }
        }
        $list = $list->orderBy('order', 'asc')->orderBy('sequence_id', 'desc')->get();
        if (empty($list)) {
            return [];
        }

        return json_decode(json_encode($list), true);
    }

    /**
     * 和EmployeeModel一对多的关系
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function employees()
    {
        return $this->hasMany(EmployeeModel::class);
    }
}
